import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;


public class Generator implements Parameters {

	/**
	 * @param args
	 * @throws IOException 
	 */
	private static Problem getOptimal(ArrayList<Problem> probArr,int id,Random randGen) throws IOException{
		int maximumX=0; 
		double maximumRevenue=0;
		int cost=randGen.nextInt(MAX_QUERY_TIME)+1;	//1-MAX_QUERY_TIME
		for(int x=0;x<TOTAL_L;x++){
			ArrayList<Problem>subSet=new ArrayList<Problem>();
			for(int i=0;i<probArr.size();++i){
				if(probArr.get(i).getResVal()<(cost+x))
					subSet.add(probArr.get(i));
			}
			double tempRevenue=getRevenue(subSet, cost, x);
			if(tempRevenue>maximumRevenue){
				maximumRevenue=tempRevenue;
				maximumX=x;
			}
		}
		
		/**
		 * revenue printing
		 *
		FileWriter fstream = new FileWriter("revenue.csv",true);
		BufferedWriter out = new BufferedWriter(fstream);
		out.append(String.valueOf(maximumRevenue));
		out.append("\n");
		out.close();
		**
		 * end max revenue
		 */
		
		Problem optimal=new Problem(id, cost, maximumX);
		return optimal;
	}
	private static double getPosition(ArrayList<Problem> arr,int id){
		for(int i=0;i<arr.size();++i){
			if(arr.get(i).getId()==id)
				return ((double)(i+1)/arr.size());
		}
		return 0;
	}
	private static int getPositionNumber(ArrayList<Problem> arr,int id){
		for(int i=0;i<arr.size();++i){
			if(arr.get(i).getId()==id)
				return (i+1);
		}
		return 0;
	}
	private static double getRevenue(ArrayList<Problem>arrP,int cost,int x){
		double revenue=1;
		for(int i=0;i<arrP.size();++i){
			revenue=revenue*(arrP.get(i).probabilitySmaller(x+cost));
		}
		revenue*=x;
		return revenue;
	}
	public static void main(String[] args) throws IOException {
	//	try
		{
		    FileWriter fstream = new FileWriter("optimal.csv",true);
			BufferedWriter out = new BufferedWriter(fstream);
			Random randGen=new Random(System.currentTimeMillis());
			for(int j=0;j<PROBLEM_SIZE;j++){
				ArrayList<Problem>problemArr=new ArrayList<Problem>();
				int k=j+1;
				String filename="servers/"+k+".srv";
				PrintWriter pw=new PrintWriter(filename);
				int numOfProbs=(randGen.nextInt(20)%18)+2;	//random 2-20
				for(int i=0;i<numOfProbs;i++){
					problemArr.add(new Problem(i+1, randGen));
				}
				
				/**
				 * add and create the optimal problem(store)
				 */
				Problem p_new=getOptimal(problemArr, numOfProbs+1, randGen);
				
				/**
				 * shuffle the stores
				 */
				int idx=randGen.nextInt(numOfProbs);
				Problem p_moved=problemArr.remove(idx);
				//changing IDs
				p_moved.setId(numOfProbs+1);
				p_new.setId(idx+1);
				//inserting the 2 problems
				problemArr.add(idx, p_new);
				problemArr.add(p_moved);
				
				//System.out.println(p_new.getId()+" "+p_new.getExpetancy()+" "+p_new.getResVal());
				/**
				 * write down the problems to the correct file
				 */
				for(int i=0;i<problemArr.size();i++){
					pw.append(problemArr.get(i).toString());
					pw.append("\n");
				}
				pw.close();
				
				Collections.sort(problemArr, new ResValComperator());
				double pos=getPosition(problemArr, p_new.getId());
				int posNum=getPositionNumber(problemArr,p_new.getId());
				Collections.sort(problemArr, new RealExecComperator());
				double lowestRealTime=problemArr.get(0).getRealExecTime();
				out.append(p_new.getId()+","+p_new.getRealExecTime()+","+pos+","+posNum+","+lowestRealTime);
				out.append("\n");
			}
			out.close();
			
			
		}
		/*
		catch (Exception e) {
			System.out.println("exception"+e.);
		}*/

	}

}
